/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import java.util.LinkedList;
import java.util.List;
import java.util.Observable;

import javax.swing.JFrame;
import javax.swing.JTextField;

import data.Request;

/**
 *
 * @author gg
 */
public class entryForm  extends Observable{//extends javax.swing.JFrame {

	public JFrame frame;
	
//	private static String url_text = "";
//	private static String max_threads = "";
	private JTextField[] kw_fields;
	
	//private List<>
	
	public entryForm(List<String> word_history){
    	frame = new JFrame();
        initComponents();
        //if we have a history
        kw_fields = new JTextField[10];
        kw_fields[0] = keyword_field_1;
        kw_fields[1] = keyword_field_2;
        kw_fields[2] = keyword_field_3;
        kw_fields[3] = keyword_field_4;
        kw_fields[4] = keyword_field_5;
        kw_fields[5] = keyword_field_6;
        kw_fields[6] = keyword_field_7;
        kw_fields[7] = keyword_field_8;
        kw_fields[8] = keyword_field_9;
        kw_fields[9] = keyword_field_10;
        
        
        		/*{ keyword_field_1, keyword_field_2, keyword_field_3, 
            	keyword_field_4, keyword_field_5, keyword_field_6, 
            	keyword_field_7, keyword_field_8, keyword_field_9, 
            	keyword_field_10
            }; */
        
        if(word_history.size() == kw_fields.length){
        	loadData(word_history);
        }
        
        frame.setTitle("choose your request - your kw data will be saved!");
        frame.setVisible(true);
	}
	
    /**
     * Creates new form entryForm
    
    public entryForm() {
    	frame = new JFrame();
        initComponents();
        loadData();
        frame.setVisible(true);
    } */

    private void loadData(List<String> words){
    	for(int i = 0; i < kw_fields.length; i++){
	    	try{
	    		kw_fields[i].setText(words.get(i));
	    	}
	    	catch(java.lang.ArrayIndexOutOfBoundsException e){
	    		kw_fields[i].setText("");
	    	}
	    }
    }
    
    private Request RequestFromData(){
    	
    	List<String> kwords = kwFieldsToList();
    	
    	String url = url_text_field.getText();
    	int threads = thread_selector_slider1.getValue();
    	//parse and get
    	int depth = Integer.parseInt(page_depth_field.getText());
    	Request requestData = new Request(kwords, url, depth, threads);
    	return requestData;
    }
    
    private List<String> kwFieldsToList(){
    	List<String> kwords = new LinkedList<String>();
    	for(int i = 0; i < kw_fields.length; i++){
    		kwords.add(kw_fields[i].getText());
    	}
    	return kwords;
    }
    
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        start_button = new javax.swing.JButton();
        url_text_field = new javax.swing.JTextField();
        keyword_field_10 = new javax.swing.JTextField();
        keywords_label = new javax.swing.JLabel();
        keyword_field_9 = new javax.swing.JTextField();
        keyword_field_8 = new javax.swing.JTextField();
        keyword_field_7 = new javax.swing.JTextField();
        keyword_field_6 = new javax.swing.JTextField();
        page_depth_field = new javax.swing.JTextField();
        url_label1 = new javax.swing.JLabel();
        jSeparator2 = new javax.swing.JSeparator();
        keyword_field_1 = new javax.swing.JTextField();
        cancel_button = new javax.swing.JButton();
        keyword_field_2 = new javax.swing.JTextField();
        keyword_field_3 = new javax.swing.JTextField();
        keyword_field_4 = new javax.swing.JTextField();
        thread_selector_slider1 = new javax.swing.JSlider();
        jSeparator1 = new javax.swing.JSeparator();
        keyword_field_5 = new javax.swing.JTextField();
        url_label = new javax.swing.JLabel();

        frame.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Threads");

        start_button.setText("Start");
        start_button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                start_buttonActionPerformed(evt);
            }
        });


        keywords_label.setText("Keywords:");


        page_depth_field.setText("100");


        url_label1.setText("Depth");

        cancel_button.setText("Cancel");
        cancel_button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancel_buttonActionPerformed(evt);
            }
        });

        thread_selector_slider1.setMajorTickSpacing(5);
        thread_selector_slider1.setMaximum(31);
        thread_selector_slider1.setMinimum(1);
        thread_selector_slider1.setMinorTickSpacing(1);
        thread_selector_slider1.setPaintLabels(true);
        thread_selector_slider1.setPaintTicks(true);
        thread_selector_slider1.setValue(15);
        thread_selector_slider1.setFocusCycleRoot(true);
        thread_selector_slider1.setName("thread_number_slider");

        url_label.setText("Url:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(frame.getContentPane());
        frame.getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(url_label, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(url_label1, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(keywords_label, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(page_depth_field, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(start_button, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 49, Short.MAX_VALUE)
                            .addComponent(cancel_button))
                        .addComponent(thread_selector_slider1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(keyword_field_5, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE)
                                .addComponent(keyword_field_4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE)
                                .addComponent(keyword_field_3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE)
                                .addComponent(keyword_field_2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE)
                                .addComponent(keyword_field_1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE))
                            .addGap(18, 18, 18)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(keyword_field_6, javax.swing.GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE)
                                .addComponent(keyword_field_8, javax.swing.GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE)
                                .addComponent(keyword_field_7, javax.swing.GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE)
                                .addComponent(keyword_field_9, javax.swing.GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE)
                                .addComponent(keyword_field_10, javax.swing.GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE)))
                        .addComponent(url_text_field, javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jSeparator2)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(url_text_field, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(url_label))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(keyword_field_1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(keyword_field_6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(7, 7, 7)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(keyword_field_2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(keyword_field_8, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(keyword_field_3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(keyword_field_7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(keyword_field_4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(keyword_field_9))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(keyword_field_10)
                            .addComponent(keyword_field_5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(page_depth_field, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(url_label1))
                        .addGap(27, 27, 27))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(keywords_label)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(thread_selector_slider1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addGap(28, 28, 28)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(start_button)
                    .addComponent(cancel_button))
                .addContainerGap())
        );

        frame.pack();
    }// </editor-fold>

    private void start_buttonActionPerformed(java.awt.event.ActionEvent evt) {
    	Request data = RequestFromData();
		cancel_buttonActionPerformed(evt);
		setChanged();
		notifyObservers(data);

    }

    private void cancel_buttonActionPerformed(java.awt.event.ActionEvent evt) {
    	frame.setVisible(false);
    	frame.dispose();
    }

    // Variables declaration - do not modify
    private javax.swing.JButton cancel_button;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JTextField keyword_field_1;
    private javax.swing.JTextField keyword_field_10;
    private javax.swing.JTextField keyword_field_2;
    private javax.swing.JTextField keyword_field_3;
    private javax.swing.JTextField keyword_field_4;
    private javax.swing.JTextField keyword_field_5;
    private javax.swing.JTextField keyword_field_6;
    private javax.swing.JTextField keyword_field_7;
    private javax.swing.JTextField keyword_field_8;
    private javax.swing.JTextField keyword_field_9;
    private javax.swing.JLabel keywords_label;
    private javax.swing.JTextField page_depth_field;
    private javax.swing.JButton start_button;
    private javax.swing.JSlider thread_selector_slider1;
    private javax.swing.JLabel url_label;
    private javax.swing.JLabel url_label1;
    private javax.swing.JTextField url_text_field;
    // End of variables declaration
}
